﻿using System;
using System.IO;
using System.Security.Cryptography;

namespace Jinndev.Utils {

    public enum HashAlgorithmType {
        MD5,
        SHA1,
        SHA256
    }

    public class HashUtil {

        public static string CalculateFileHash(string filePath, HashAlgorithmType hashAlgorithmType = HashAlgorithmType.MD5) {
            using (var hashAlgorithm = GetHashAlgorithm(hashAlgorithmType)) {
                using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) {
                    byte[] hashBytes = hashAlgorithm.ComputeHash(fileStream);
                    return BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
                }
            }
        }

        public static HashAlgorithm GetHashAlgorithm(HashAlgorithmType hashAlgorithmType) {
            switch (hashAlgorithmType) {
                case HashAlgorithmType.MD5:
                    return MD5.Create();
                case HashAlgorithmType.SHA1:
                    return SHA1.Create();
                case HashAlgorithmType.SHA256:
                    return SHA256.Create();
                // 可以添加其他哈希算法支持
                default:
                    throw new ArgumentException("不支持的哈希算法类型。", nameof(hashAlgorithmType));
            }
        }

    }
}
